package com.liuqianfei.util;

import java.util.Arrays;

/**
 * @author liuqianfei
 */
public class ArrayUtil
{
    private static String[] EMPTY_STRING_ARRAY = new String[0];

    public static <T> int indexOf(T[] array, T t)
    {
        int index = -1;
        for (int i = 0; i < array.length; i++)
        {
            T temp = array[i];
            if (null != t && t.equals(temp))
                return i;
        }
        return index;
    }

    public static String[] slice(String[] array, int start)
    {
        if (null == array)
            return EMPTY_STRING_ARRAY;

        int size = array.length;

        if (start < 0)
            start += size;

        if (start < 0)
            start = 0;

        if (start >= size)
            start = size;

        int newSize = size - start;
        String[] newArray = new String[newSize];
        System.arraycopy(array, start, newArray, 0, newSize);

        return newArray;
    }

    public static String[] slice(String[] array, int start, int end)
    {
        if (null == array)
            return EMPTY_STRING_ARRAY;

        int size = array.length;

        // convert to positive
        if (start < 0)
            start += size;

        if (end < 0)
            end += size;

        // assert start < end
        if (start >= end)
            return EMPTY_STRING_ARRAY;

        // check array bound
        if (start < 0)
            start = 0;
        if (end < 0)
            end = 0;

        if (start >= size)
            start = size;
        if (end >= size)
            end = size;

        // copy
        int newSize = end - start;
        String[] newArray = new String[newSize];
        System.arraycopy(array, start, newArray, 0, newSize);

        return newArray;
    }

    public static  <T> T[] set(T[] array, int index, T t)
    {
        if (index < 0)
            return array;

        int length = array.length;

        if (index < length)
        {
            array[index] = t;
            return array;
        }
        else
        {
            T[] newArray = Arrays.copyOf(array, index + 1);
            newArray[index] = t;
            return newArray;
        }
    }

    public static char[] set(char[] array, int index, char t)
    {
        if (index < 0)
            return array;

        int length = array.length;

        if (index < length)
        {
            array[index] = t;
            return array;
        }
        else
        {
            char[] newArray = Arrays.copyOf(array, index + 1);
            newArray[index] = t;
            return newArray;
        }
    }

    public static <T> T[] push(T[] array, T t)
    {
        int newLength = array.length + 1;
        T[] newArray = Arrays.copyOf(array, newLength);
        newArray[newLength - 1] = t;
        return newArray;
    }

    public static char[] push(char[] array, char t)
    {
        int newLength = array.length + 1;
        char[] newArray = Arrays.copyOf(array, newLength);
        newArray[newLength - 1] = t;
        return newArray;
    }

    public static <T> T[] unshift(T[] array, T... t)
    {
        return concat(t, array);
    }

    public static char[] unshift(char[] array, char... t)
    {
        return concat(t, array);
    }

    public static <T> T[] concat(T[] first, T[] second)
    {
        T[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

    public static char[] concat(char[] first, char[] second)
    {
        char[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

    public static <T> void print(String message, T[] array)
    {
        System.out.print(message);
        if (array.length > 0)
            System.out.print(array[0]);
        for (int i = 1; i < array.length; i++)
        {
            System.out.print("," + array[i]);
        }
        System.out.println();
    }

    public static void print(String message, char[] array)
    {
        System.out.print(message);
        if (array.length > 0)
            System.out.print(array[0]);
        for (int i = 1; i < array.length; i++)
        {
            System.out.print("," + array[i]);
        }
        System.out.println();
    }
}
